<?php
if (!function_exists('domainWhiteList')) {
    /**
     * 域名白名单
     *
     * @param  array $white_list 域名白名单
     * @param  bool $merge 是否合并默认的白名单
     * @return bool
     */
    function domainWhiteList(array $white_list = [], bool $merge = true)
    {
        // 默认白名单
        $default_white_list = [
            'http://www.adpush.com',
            'http://easyvote.1949cn.top',
            'https://easyvote.1949cn.top',
        ];

        // 是否合并默认的白名单
        if ($merge === true) {
            $white_list = array_merge($default_white_list, $white_list);
        }

        // 当前访问域名
        $current_origin = isset($_SERVER['HTTP_ORIGIN']) ?  $_SERVER['HTTP_ORIGIN'] : '';

        // 域名是否在白名单中
        if (in_array($current_origin, $white_list)) {
            header("content-type:text/html;charset=utf-8");
            header("Access-Control-Allow-Origin:" . $current_origin);
            header("Access-Control-Allow-Headers:Origin,X-Requested-With,Content-Type,Accept");
            header('Access-Control-Allow-Methods:GET,POST,PUT');
        }
    }
}